package com.augn.service.impl;

import com.augn.entity.Post;
import com.augn.entity.Result;
import com.augn.mapper.PostMapper;
import com.augn.service.IPostService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author augn
 * @since 2022-09-22
 */
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements IPostService {
    @Autowired
    private PostMapper mapper;

    // 新增一个帖子
    @Override
    public Result<?> insertOnePost(Post post) {
        int insert = mapper.insert(post);
        if(insert==0){
            return Result.error("-1","新增失败");
        } else{
            return Result.success(insert);
        }
    }

    //删除一个帖子
    @Override
    public Result<?> deleteOnePost(Integer id) {
        int delete = mapper.deleteById(id);
        if(delete==0){
            return Result.error("-1","删除失败");
        } else{
            return Result.success(delete);
        }
    }

    @Override
    public Result<?> updateOnePost(Post post) {
        int update = mapper.updateById(post);
        if(update==0){
            return Result.error("-1","更新失败");
        } else{
            return Result.success(update);
        }
    }

    //获取一个帖子
    @Override
    public Result<?> getOnePost(Integer id) {
        Post post = mapper.getOnePost(id);
        if(post==null){
            return Result.error("-1","无此帖子");
        } else{
            return Result.success(post);
        }
    }

    //获取符合条件的帖子列表
    @Override
    public Result<?> getPartPost(Post post,Integer pageNum,Integer pageSize) {
        QueryWrapper<Post> wrapper= new QueryWrapper<>();
        if (post.getUserId()!=null) {
            wrapper.eq("user_id",post.getUserId());
        }
        if (post.getRole()!=null) {
            wrapper.eq("role",post.getRole());
        }
        if (post.getSort()!=null&&!post.getSort().equals("")) {
            wrapper.like("sort",post.getSort());
        }

        if (post.getNotSort()!=null&&!post.getNotSort().equals("")) {
            wrapper.notLike("sort",post.getNotSort());
        }

        if (post.getDetail()!=null) {
            wrapper.and(i ->i.like("title",post.getDetail())
                    .or().like("sort",post.getDetail())
                    .or().like("detail",post.getDetail()));
        }

        long pages ,total;
        List<Post> postList;
        Map<String,Object> ret = new HashMap<>();
        if((pageNum==null&&pageSize==null)||(pageNum==null&&pageSize!=null)){
            postList =  mapper.selectList(wrapper);
            ret.put("postList",postList);
        } else {
            if(pageSize==null){
                pageSize=20;
            }
            Page<Post> postPage = new Page<>(pageNum , pageSize);
            IPage<Post> postIPage = mapper.selectPage(postPage , wrapper);
            postList = postIPage.getRecords();
            pages = postIPage.getPages();
            total = postIPage.getTotal();
            ret.put("postList",postList);
            ret.put("pages",pages);
            ret.put("total",total);
        }

        if(postList.isEmpty()){
            return Result.error("-1","没有符合此条件的帖子");
        } else{
            return Result.success(ret);
        }
    }
}
